/*
 * 
 */
package Grafik;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import root.Controler;

/**
 * @author edgarjulca & Fana
 * @version 1.0
 */
public class GrafikRectangle extends GrafikObject {

    private GrafikPoint a;
    private GrafikPoint b;
    private GrafikPoint c;
    private GrafikPoint d;
    private int windowWith = 0;
    private int windowHeight = 0;
    private int offset = 10;

    public GrafikRectangle(int xa, int ya, int xb, int yb, int xc, int yc, int xd, int yd) {
        this.a = new GrafikPoint(xa, ya);
        this.b = new GrafikPoint(xb, yb);
        this.c = new GrafikPoint(xc, yc);
        this.d = new GrafikPoint(xd, yd);

    }
    /*
     * Setter
     */
    public void setPunktA(GrafikPoint a) {
        this.a = a;
    }

    public void setPunktB(GrafikPoint b) {
        this.b = b;
    }

    public void setPunktC(GrafikPoint c) {
        this.c = c;
    }

    public void setPunktd(GrafikPoint d) {
        this.d = d;
    }

    public void setWindowWith(int windowWith) {
        this.windowWith = windowWith;
    }

    public void setWindowHeight(int windowHeight) {
        this.windowHeight = windowHeight;
    }
    /*
     * getter
     */
    public GrafikPoint getA() {
        return this.a;
    }

    public GrafikPoint getB() {
        return this.b;
    }

    public GrafikPoint getC() {
        return this.c;
    }

    public GrafikPoint getD() {
        return this.d;
    }

    public int getWindowWith() {
        return windowWith;
    }

    public int getWindowHeight() {
        return windowHeight;
    }
    
    /*
     * Methode zum Zeichnen des kreises mit seinem  Rotem Mittelpunkt
     * Die Eingegebenen Punkten werden durch die methode @see #methode sort_points()
     *  @param raphics2D g, dies ist ein Graphics2D objekt was von @see 
     *  @param Color color, farbe des mittelpunktes(Color.RED)
     */
    @Override
    public void draw(Graphics2D g, Color color) {
        sort_points();
        g.drawLine(a.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - a.getY() - ((int) (offset / Controler.getScale())), b.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - b.getY() - ((int) (offset / Controler.getScale())));
        g.drawLine(b.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - b.getY() - ((int) (offset / Controler.getScale())), c.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - c.getY() - ((int) (offset / Controler.getScale())));
        g.drawLine(c.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - c.getY() - ((int) (offset / Controler.getScale())), d.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - d.getY() - ((int) (offset / Controler.getScale())));
        g.drawLine(d.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - d.getY() - ((int) (offset / Controler.getScale())), a.getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - a.getY() - ((int) (offset / Controler.getScale())));
        drawPixel(g, getCenter().getX() + ((int) (offset / Controler.getScale())), ((int) (windowHeight / Controler.getScale())) - getCenter().getY() - ((int) (offset / Controler.getScale())), Color.RED);
    }
    /*
     * Methode zur ermittlung des Mittelpunktes
     */
    @Override
    public GrafikPoint getCenter() {
        GrafikPoint center;
        int x = 0;
        int y = 0;
        if (this.getA().getX() != this.getB().getX()) {
            x = (this.getA().getX() + this.getB().getX()) / 2;
        } else if (this.getA().getX() != this.getC().getX()) {
            x = (this.getA().getX() + this.getC().getX()) / 2;
        } else if (this.getA().getX() != this.getD().getX()) {
            x = (this.getA().getX() + this.getD().getX()) / 2;
        }

        if (this.getA().getY() != this.getB().getY()) {
            y = (this.getA().getY() + this.getB().getY()) / 2;
        } else if (this.getA().getY() != this.getC().getY()) {
            y = (this.getA().getY() + this.getC().getY()) / 2;
        } else if (this.getA().getY() != this.getD().getY()) {
            y = (this.getA().getY() + this.getD().getY()) / 2;
        }
        center = new GrafikPoint(x, y);
        return center;

    }
    /*
     * Methode zur sortierung der Punkte 
     */
    public void sort_points() {
        int tmp_x;
        int tmp_y;
        if (this.getA().getX() < this.getB().getX() && this.getA().getY() < this.getB().getY()
                || this.getA().getX() < this.getB().getX() && this.getA().getY() > this.getB().getY()
                || this.getA().getX() > this.getB().getX() && this.getA().getY() > this.getB().getY()
                || this.getA().getX() > this.getB().getX() && this.getA().getY() < this.getB().getY()) {
            tmp_x = this.getB().getX();
            tmp_y = this.getB().getY();

            this.getB().setX(this.getC().getX());
            this.getB().setY(this.getC().getY());

            this.getC().setX(tmp_x);
            this.getC().setY(tmp_y);

        } else if (this.getB().getX() < this.getC().getX() && this.getB().getY() < this.getC().getY()
                || this.getB().getX() < this.getC().getX() && this.getB().getY() > this.getC().getY()
                || this.getB().getX() > this.getC().getX() && this.getB().getY() > this.getC().getY()
                || this.getB().getX() > this.getC().getX() && this.getB().getY() < this.getC().getY()) {
            tmp_x = this.getA().getX();
            tmp_y = this.getA().getY();

            this.getA().setX(this.getB().getX());
            this.getA().setY(this.getB().getY());

            this.getB().setX(tmp_x);
            this.getB().setY(tmp_y);
        } else if (this.getC().getX() < this.getD().getX() && this.getC().getY() < this.getD().getY()
                || this.getC().getX() < this.getD().getX() && this.getC().getY() > this.getD().getY()
                || this.getC().getX() > this.getD().getX() && this.getC().getY() > this.getD().getY()
                || this.getC().getX() > this.getD().getX() && this.getC().getY() < this.getD().getY()) {
            tmp_x = this.getB().getX();
            tmp_y = this.getB().getY();

            this.getB().setX(this.getC().getX());
            this.getB().setY(this.getC().getY());

            this.getC().setX(tmp_x);
            this.getC().setY(tmp_y);
        }
    }
    /*
     * Methode zur ueberpruefung ob die eingaben der X/Y werte
     * zu einem rechteck gehoeren
     */
    public boolean istRechteck() {

        double AB;
        double BC;
        double CD;
        double DA;

        AB = Math.sqrt(Math.pow(this.getA().getX() - this.getB().getX(), 2) + Math.pow(this.getA().getY() - this.getB().getY(), 2));
        BC = Math.sqrt(Math.pow(this.getB().getX() - this.getC().getX(), 2) + Math.pow(this.getB().getY() - this.getC().getY(), 2));
        CD = Math.sqrt(Math.pow(this.getC().getX() - this.getD().getX(), 2) + Math.pow(this.getC().getY() - this.getD().getY(), 2));
        DA = Math.sqrt(Math.pow(this.getD().getX() - this.getA().getX(), 2) + Math.pow(this.getD().getY() - this.getA().getY(), 2));
        if (AB == CD && BC == DA) {
            return true;
        }

        return false;

    }
    
    /*
     * Methode zur ueberpruefung ob die rechtecke sich ueberschneiden
     */
    public boolean checkEdges() {
        ArrayList<GrafikObject> objects = Controler.getInstance().getObjects();
        int gleichePunkte = 0;

        if (objects.isEmpty()) {
            if ((this.getA().getX() == 0 && this.getA().getY() == 0)
                    || (this.getB().getX() == 0 && this.getB().getY() == 0)
                    || (this.getC().getX() == 0 && this.getC().getY() == 0)
                    || (this.getD().getX() == 0 && this.getD().getY() == 0)) {
                return true;
            }
            return false;
        }

        for (GrafikObject object : objects) {


            if (object instanceof GrafikRectangle) {
                GrafikRectangle obj = (GrafikRectangle) object;
                if (obj.getA().getX() == this.getA().getX() && obj.getA().getY() == this.getA().getY()
                        || obj.getB().getX() == this.getA().getX() && obj.getB().getY() == this.getA().getY()
                        || obj.getC().getX() == this.getA().getX() && obj.getC().getY() == this.getA().getY()
                        || obj.getD().getX() == this.getA().getX() && obj.getD().getY() == this.getA().getY()) {
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getB().getX() && obj.getA().getY() == this.getB().getY()
                        || obj.getB().getX() == this.getB().getX() && obj.getB().getY() == this.getB().getY()
                        || obj.getC().getX() == this.getB().getX() && obj.getC().getY() == this.getB().getY()
                        || obj.getD().getX() == this.getB().getX() && obj.getD().getY() == this.getB().getY()) {
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getC().getX() && obj.getA().getY() == this.getC().getY()
                        || obj.getB().getX() == this.getC().getX() && obj.getB().getY() == this.getC().getY()
                        || obj.getC().getX() == this.getC().getX() && obj.getC().getY() == this.getC().getY()
                        || obj.getD().getX() == this.getC().getX() && obj.getD().getY() == this.getC().getY()) {
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getD().getX() && obj.getA().getY() == this.getD().getY()
                        || obj.getB().getX() == this.getD().getX() && obj.getB().getY() == this.getD().getY()
                        || obj.getC().getX() == this.getD().getX() && obj.getC().getY() == this.getD().getY()
                        || obj.getD().getX() == this.getD().getX() && obj.getD().getY() == this.getD().getY()) {
                    gleichePunkte++;
                }
            }

            if (object instanceof GrafikTriangle) {
                GrafikTriangle obj = (GrafikTriangle) object;
                if (obj.getA().getX() == this.getA().getX() && obj.getA().getY() == this.getA().getY()
                        || obj.getB().getX() == this.getA().getX() && obj.getB().getY() == this.getA().getY()
                        || obj.getC().getX() == this.getA().getX() && obj.getC().getY() == this.getA().getY()) {
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getB().getX() && obj.getA().getY() == this.getB().getY()
                        || obj.getB().getX() == this.getB().getX() && obj.getB().getY() == this.getB().getY()
                        || obj.getC().getX() == this.getB().getX() && obj.getC().getY() == this.getB().getY()) {
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getC().getX() && obj.getA().getY() == this.getC().getY()
                        || obj.getB().getX() == this.getC().getX() && obj.getB().getY() == this.getC().getY()
                        || obj.getC().getX() == this.getC().getX() && obj.getC().getY() == this.getC().getY()) {
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getD().getX() && obj.getA().getY() == this.getD().getY()
                        || obj.getB().getX() == this.getD().getX() && obj.getB().getY() == this.getD().getY()
                        || obj.getC().getX() == this.getD().getX() && obj.getC().getY() == this.getD().getY()) {
                    gleichePunkte++;
                }
            }
            if (gleichePunkte >= 2) {
                return true;
            }
        }
        return false;
    }
}
